from django.db import models
from django.contrib.auth.models import AbstractUser

class User(AbstractUser):
    # 移除默认的 username 字段
    username = None
    user_id = models.AutoField(primary_key=True)
    phone = models.CharField(max_length=20, unique=True)
    registration_time = models.DateTimeField(auto_now_add=True)
    is_admin = models.BooleanField(default=False)
    email = models.EmailField(unique=True)

    # 设置 USERNAME_FIELD 和 REQUIRED_FIELDS
    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def __str__(self):
        return self.email

class Movie(models.Model):
    # 移除自定义的 movie_id 字段，使用默认的 id 字段
    title = models.CharField(max_length=255)
    genre = models.CharField(max_length=255)
    duration = models.IntegerField()
    director = models.CharField(max_length=255)
    actors = models.TextField()
    release_date = models.DateField()
    rating = models.DecimalField(max_digits=3, decimal_places=2)

class Hall(models.Model):
    # 使用默认的 id 作为主键
    hall_name = models.CharField(max_length=50)
    seat_count = models.IntegerField()
    seat_layout = models.CharField(max_length=20)

class Schedule(models.Model):
    movie = models.ForeignKey(Movie, on_delete=models.CASCADE)
    cinema_id = models.IntegerField()
    hall = models.ForeignKey(Hall, on_delete=models.CASCADE)
    screening_time = models.DateTimeField()
    price = models.DecimalField(max_digits=10, decimal_places=2)

class Seat(models.Model):
    seat_id = models.AutoField(primary_key=True)
    hall = models.ForeignKey(Hall, on_delete=models.CASCADE)
    schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)
    seat_number = models.CharField(max_length=10)
    status = models.CharField(max_length=10, choices=[('空闲', '空闲'), ('已售', '已售')])

class Order(models.Model):
    order_id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    schedule = models.ForeignKey(Schedule, on_delete=models.CASCADE)
    seat = models.ForeignKey(Seat, on_delete=models.CASCADE)
    purchase_time = models.DateTimeField(auto_now_add=True)
    total_amount = models.DecimalField(max_digits=10, decimal_places=2)